package com.cmc6.menu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc6.common.model.dto.AddOrderNoDTO;
import com.cmc6.common.model.dto.NotEmptyIdSet;
import com.cmc6.common.model.dto.NotNullId;
import com.cmc6.common.model.entity.MenuDO;
import com.cmc6.common.util.ApiResult;
import com.cmc6.menu.model.dto.MenuInsertOrUpdateDTO;
import com.cmc6.menu.model.dto.MenuPageDTO;
import com.cmc6.menu.model.vo.MenuInfoByIdVO;
import com.cmc6.menu.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "菜单控制器")
@RequestMapping("/menu")
@RestController
public class MenuController {

    @Resource
    MenuService baseService;

    @ApiOperation(value = "新增/修改 菜单")
    @PostMapping("/insertOrUpdate")
    @PreAuthorize("hasAuthority('menu:insertOrUpdate')")
    public ApiResult<String> insertOrUpdate(@RequestBody @Valid MenuInsertOrUpdateDTO dto) {
        return ApiResult.ok(baseService.insertOrUpdate(dto));
    }

    @ApiOperation(value = "分页排序查询：菜单")
    @PostMapping("/page")
    @PreAuthorize("hasAuthority('menu:page')")
    public ApiResult<Page<MenuDO>> myPage(@RequestBody @Valid MenuPageDTO dto) {
        return ApiResult.ok(baseService.myPage(dto));
    }

    @ApiOperation(value = "查询菜单（树结构）")
    @PostMapping("/tree")
    @PreAuthorize("hasAuthority('menu:page')")
    public ApiResult<List<MenuDO>> tree(@RequestBody @Valid MenuPageDTO dto) {
        return ApiResult.ok(baseService.tree(dto));
    }

    @ApiOperation(value = "删除菜单")
    @PostMapping("/deleteByIdSet")
    @PreAuthorize("hasAuthority('menu:deleteByIdSet')")
    public ApiResult<String> deleteByIdSet(@RequestBody @Valid NotEmptyIdSet notEmptyIdSet) {
        return ApiResult.ok(baseService.deleteByIdSet(notEmptyIdSet));
    }

    @ApiOperation(value = "通过主键id，查看详情")
    @PostMapping("/infoById")
    @PreAuthorize("hasAuthority('menu:infoById')")
    public ApiResult<MenuInfoByIdVO> infoById(@RequestBody @Valid NotNullId notNullId) {
        return ApiResult.ok(baseService.infoById(notNullId));
    }

    @PostMapping("/user")
    @ApiOperation(value = "获取当前用户绑定的菜单")
    public ApiResult<List<MenuDO>> userMenuList() {
        return ApiResult.ok(baseService.userMenuList());
    }

    @ApiOperation(value = "通过主键 idSet，加减排序号")
    @PostMapping("/addOrderNo")
    @PreAuthorize("hasAuthority('menu:insertOrUpdate')")
    public ApiResult<String> addOrderNo(@RequestBody @Valid AddOrderNoDTO dto) {
        return ApiResult.ok(baseService.addOrderNo(dto));
    }

}
